import pymysql
def main():
    connect = pymysql.connect(host="39.107.245.102",port=3306,user="root",passwd="wuji5201314.",db="school",charset="utf8")
    try:
        with connect.cursor() as cursor:
            # 查询语句不进行赋值
            cursor.execute("select stu_id,stu_name,stu_stuaddr from tb_student")
            for row in cursor.fetchall():
                # cursor.fetchall()以元组的形式取值，取所有值
                # cursor.fetchone()以元组的形式取值，只取一个值
                # cursor.fetchmany(数量)以元组的形式取值，取n个值
        # 注意：游标会随着fetch的数量而浮动
                print("学生编号：{}".format(row[0]))
                print("学生姓名：{}".format(row[1]))
                print("学生住址：{}".format(row[2]))
                print('*'*20)

# 当我们使用fetch的时候，拿到的数据是大元组包着小元组，真实的环境都是想要拿到大元组包裹着字典
    except pymysql.MySQLError as error:
        print('查询失败.{}'.format(error))
    # finally:正常异常都执行，必须要释放资源
    finally:
        connect.close()
if __name__=="__main__":
    main()